package com.woniuxy.project115.kaer.tenant.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.project115.kaer.commons.util.JwtUtil;
import com.woniuxy.project115.kaer.commons.util.Result;
import com.woniuxy.project115.kaer.tenant.form.AddServiceInfoForm;
import com.woniuxy.project115.kaer.tenant.form.GetServiceInfoForm;
import com.woniuxy.project115.kaer.tenant.form.SelectServiceInfoForm;
import com.woniuxy.project115.kaer.tenant.param.AddServiceInfoParam;
import com.woniuxy.project115.kaer.tenant.param.GetServiceInfoParam;
import com.woniuxy.project115.kaer.tenant.param.SelectServiceInfoParam;
import com.woniuxy.project115.kaer.tenant.service.ServiceInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuH
 * @since 2025年02月19日
 */
@RestController
@RequestMapping("/serviceInfo")
@Api(tags = "服务商品信息接口")
public class ServiceInfoController {
    @Resource
    private ServiceInfoService serviceInfoService;

    @PostMapping("/uploadImage")
    @ApiOperation("商家上传服务商品图片")
    public Result<String> uploadImage(MultipartFile file) throws Exception {
        return Result.succeed("上传成功", serviceInfoService.uploadImage(file));
    }

    @PostMapping("/addServiceInfo")
    @ApiOperation("新增服务商品信息")
    public Result addServiceInfo(@RequestBody AddServiceInfoForm form, @RequestHeader String token) throws Exception {
        Integer tenantInfoId = JwtUtil.getTenantInfoId(token);
        serviceInfoService.addServiceInfo(BeanUtil.toBean(form, AddServiceInfoParam.class), tenantInfoId);
        return Result.succeed("新增成功");
    }

    @PostMapping("/online")
    @ApiOperation("上架服务商品信息")
    @ApiImplicitParam(name = "serviceInfoId", value = "服务商品信息ID")
    public Result online(Integer serviceInfoId) throws Exception {
        serviceInfoService.online(serviceInfoId);
        return Result.succeed("上架成功");
    }

    @PostMapping("/offline")
    @ApiOperation("下架服务商品信息")
    @ApiImplicitParam(name = "serviceInfoId", value = "服务商品信息ID")
    public Result offline(Integer serviceInfoId) throws Exception {
        serviceInfoService.offline(serviceInfoId);
        return Result.succeed("下架成功");
    }

    @PostMapping("/selectServiceInfo")
    @ApiOperation("商家查询服务商品信息")
    public Result selectServiceInfo(@RequestBody SelectServiceInfoForm form, @RequestHeader String token) throws Exception {
        Integer storeId = JwtUtil.getStoreId(token);
        return Result.succeed("success", serviceInfoService.selectServiceInfo(BeanUtil.toBean(form, SelectServiceInfoParam.class), storeId));
    }

    //用户根据店铺id搜索服务商品
    @PostMapping("/getServiceInfoByStoreId")
    @ApiOperation("用户根据店铺id搜索服务商品")
    public Result getServiceInfoByStoreId(@RequestBody GetServiceInfoForm form, @RequestHeader String token) throws Exception {
        Integer userId = JwtUtil.getUserId(token);
        return Result.succeed("success", serviceInfoService.getServiceInfoByStoreId(BeanUtil.toBean(form, GetServiceInfoParam.class)));
    }
}

